You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[...nextauth].ts 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. import NextAuth from 'next-auth'
  3. import { signin } from 'next-auth/client'
  4. import Providers from 'next-auth/providers'
  5. export default function (req: NextApiRequest, res: NextApiResponse) {
  6. return NextAuth(req, res, {
  7. providers: [
  8. Providers.GitHub({
  9. clientId: process.env.GITHUB_ID,
  10. clientSecret: process.env.GITHUB_SECRET,
  11. }),
  12. ],
  13. callbacks: {
  14. async redirect(url, baseUrl) {
  15. return url.startsWith(baseUrl) ? url : baseUrl
  16. },
  17. async session(session, token) {
  18. // @ts-ignore
  19. session.user.id = token.id
  20. return session
  21. },
  22. async signIn(user, account, profile) {
  23. // @ts-ignore
  24. const canLogin = await isSponsoringMe(profile?.login)
  25. if (canLogin) {
  26. return canLogin
  27. } else {
  28. return '/sponsorware'
  29. }
  30. },
  31. },
  32. })
  33. }
  34. const whitelist = ['steveruizok']
  35. async function isSponsoringMe(login: string) {
  36. if (whitelist.includes(login)) return true
  37. const res = await fetch('https://api.github.com/graphql', {
  38. method: 'POST',
  39. headers: {
  40. 'Content-Type': 'application/json',
  41. Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
  42. },
  43. body: JSON.stringify({
  44. query: `
  45. query {
  46. user(login: "steveruizok") {
  47. isSponsoredBy(accountLogin: "${login}")
  48. }
  49. }
  50. `,
  51. }),
  52. }).then((res) => res.json())
  53. return res?.data?.user?.isSponsoredBy
  54. }